﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CarLoanSystem.Entity;
using System.Data;
using CarLoanSystem.DAL;
using CarLoanSystem.Web.Comman;

namespace CarLoanSystemWeb.Page.Loan
{
    public partial class wuc_loan_member : UserControlManageForm
    {
        private static CustomerItem cusItem;
        private static CarInfoItem carItem;
        private static InterrestInfoItem InterrestItem;
        private static decimal downPay;
        private static decimal totalAmount;
        public event EventHandler InfoMessege;
        public event EventHandler ErrorMessege;
        public event EventHandler ErrorMessegeName;
        public event EventHandler ErrorMessegeBlackList;
        public event EventHandler ErrorMessegePayment;
        //public event EventHandler ErrorMessegeDept;
        public static string message;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cusItem = Session["Member"] as CustomerItem;
                ddlBrand.AppendDataBoundItems = true;
                ddlDownPay.AppendDataBoundItems = true;
                ddlEngine.AppendDataBoundItems = true;
                ddlModel.AppendDataBoundItems = true;
                ddlMonth.AppendDataBoundItems = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (cusItem.CUSTOMER_BLACK_LIST == "N")
                {
                    if (LoanInfoDAL.CheckLoan(cusItem.CUSTOMER_ID))
                    {
                        if (LoanInfoDAL.CheckFinishPayMonth(cusItem.CUSTOMER_ID))
                        {
                            LoanItem loanItem = new LoanItem();
                            loanItem.CAR_ID = Convert.ToInt32(ddlEngine.SelectedValue.ToString());
                            loanItem.CUSTOMER_ID = cusItem.CUSTOMER_ID;
                            loanItem.DEBT_MONTH_REST = Convert.ToDecimal(txtPriceInterest.Text.ToString());
                            loanItem.DEBT_STATUS = string.Empty;
                            loanItem.DOWN_PAYMENT = Convert.ToDecimal(txtPay.Text.ToString());
                            loanItem.DOWN_PAYMENT_ID = Convert.ToInt32(ddlDownPay.SelectedValue.ToString());
                            loanItem.INTERREST_ID = Convert.ToInt32(ddlMonth.SelectedValue.ToString());
                            loanItem.LOAN_DATE = DateTime.Now;
                            loanItem.LOAN_STATUS = "A";
                            loanItem.PAY_MONTH = Convert.ToDecimal(txtPayMonth.Text.ToString());
                            loanItem.VEHICLE_ID = "";
                            loanItem.PRINCIPLE = carItem.CAR_PRICE - loanItem.DOWN_PAYMENT;
                            LoanInfoDAL.Insert(loanItem);

                            ScheduleItem scheduleItem = new ScheduleItem();
                            scheduleItem.SCHEDULE_JOB = "L";
                            scheduleItem.SCHEDULE_STATUS = "A";
                            scheduleItem.EMPLOYEE_ID = ScheduleInfoDAL.LoadEmpJobLoan();
                            scheduleItem.LOAN_ID = LoanInfoDAL.LoadLoanID(cusItem.CUSTOMER_ID);
                            ScheduleInfoDAL.Insert(scheduleItem);

                            EmployeeItem emp = EmployeeInfoDAL.LoadEmployee(scheduleItem.EMPLOYEE_ID.ToString());
                            AlertsMessageItem alertItem = new AlertsMessageItem();
                            alertItem.ALERTS_MESSAGE_DETAIL = "ทำเรื่องขอกู้เงินรหัส " + scheduleItem.LOAN_ID + " ของลูกค้าชื่อ " + cusItem.CUSTOMER_NAME + " " + cusItem.CUSTOMER_LASTNAME;
                            alertItem.ALERTS_MESSAGE_STATUS = "A";
                            alertItem.USER_NAME = emp.USERNAME;
                            AlertsMessageInfoDAL.Insert(alertItem);

                            loanItem.LOAN_ID = LoanInfoDAL.LoadLoanID(cusItem.CUSTOMER_ID);
                            int loop = 7; //จำนวนวันที่บวกเพื่อเริ่มคำนวณวันนัดหมาย
                            AppointmentsItem appItem = new AppointmentsItem();
                            appItem.LOAN_ID = loanItem.LOAN_ID;
                            appItem.APPOINTMENTS_DATE = DateTime.Now.AddDays(loop);
                            appItem.APPOINTMENTS_DATE.Date.AddYears(-543);
                            int queInDay = AppointmentsInfoDAL.LoadQue(appItem.APPOINTMENTS_DATE.Year + "-" + appItem.APPOINTMENTS_DATE.Month + "-" + appItem.APPOINTMENTS_DATE.Day, emp.EMPLOYEE_ID);
                            do
                            {
                                if (queInDay <= 6)
                                {
                                    if (queInDay == 0)
                                    {
                                        appItem.APPOINTMENTS_START_TIME = "09:00";
                                        appItem.APPOINTMENTS_END_TIME = "10:00";
                                    }
                                    else if (queInDay == 1)
                                    {
                                        appItem.APPOINTMENTS_START_TIME = "10:00";
                                        appItem.APPOINTMENTS_END_TIME = "11:00";
                                    }
                                    else if (queInDay == 2)
                                    {
                                        appItem.APPOINTMENTS_START_TIME = "11:00";
                                        appItem.APPOINTMENTS_END_TIME = "12:00";
                                    }
                                    else if (queInDay == 3)
                                    {
                                        appItem.APPOINTMENTS_START_TIME = "13:00";
                                        appItem.APPOINTMENTS_END_TIME = "14:00";
                                    }
                                    else if (queInDay == 4)
                                    {
                                        appItem.APPOINTMENTS_START_TIME = "14:00";
                                        appItem.APPOINTMENTS_END_TIME = "15:00";
                                    }
                                    else if (queInDay == 5)
                                    {
                                        appItem.APPOINTMENTS_START_TIME = "15:00";
                                        appItem.APPOINTMENTS_END_TIME = "16:00";
                                    }
                                    else if (queInDay == 6)
                                    {
                                        appItem.APPOINTMENTS_START_TIME = "16:00";
                                        appItem.APPOINTMENTS_END_TIME = "17:00";
                                    }
                                }
                                else
                                {
                                    loop = loop + 1;
                                    appItem.APPOINTMENTS_DATE = DateTime.Now.AddDays(loop);
                                    queInDay = AppointmentsInfoDAL.LoadQue(appItem.APPOINTMENTS_DATE.Year + "-" + appItem.APPOINTMENTS_DATE.Month + "-" + appItem.APPOINTMENTS_DATE.Day, emp.EMPLOYEE_ID);
                                }
                            } while (appItem.APPOINTMENTS_START_TIME.Equals("") || appItem.APPOINTMENTS_END_TIME.Equals(""));
                            appItem.APPOINTMENTS_STATUS = "A";
                            AppointmentsInfoDAL.Insert(appItem);

                            alertItem = new AlertsMessageItem();
                            alertItem.ALERTS_MESSAGE_DETAIL = "แจ้งเตือนการนัดหมายการขอกู้วัน " + appItem.APPOINTMENTS_DATE.ToShortDateString() + " เวลา "
                                + appItem.APPOINTMENTS_START_TIME + " - " + appItem.APPOINTMENTS_END_TIME + " น. กับพนักงานชื่อ "
                                + emp.EMPLOYEE_NAME + " " + emp.EMPLOYEE_LASTNAME;
                            alertItem.ALERTS_MESSAGE_STATUS = "C";
                            alertItem.USER_NAME = cusItem.USERNAME;
                            alertItem.LOAN_ID = appItem.LOAN_ID;
                            AlertsMessageInfoDAL.Insert(alertItem);

                            if (InfoMessege != null)
                            {
                                InfoMessege(this, EventArgs.Empty);
                            }
                        }
                        else
                        {
                            if (ErrorMessegePayment != null)
                            {
                                ErrorMessegePayment(this, EventArgs.Empty);
                            }
                        }
                    }
                    else
                    {
                        if (ErrorMessegeName != null)
                        {
                            ErrorMessegeName(this, EventArgs.Empty);
                        }
                    }
                }
                else
                {
                    if (ErrorMessegeBlackList != null)
                    {
                        ErrorMessegeBlackList(this, EventArgs.Empty);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ErrorMessege != null)
                {
                    ErrorMessege(this, EventArgs.Empty);
                }
            }
        }

        protected void ddlBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataSource2.DataBind();
            SqlDataSource3.DataBind();
        }

        protected void ddlModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataSource3.DataBind();
        }

        protected void ddlEngine_SelectedIndexChanged(object sender, EventArgs e)
        {
            carItem = CarInfoDAL.LoadCar(Convert.ToInt32(this.ddlEngine.SelectedValue.ToString()));
            this.txtPrice.Text = carItem.CAR_PRICE.ToString();
        }

        protected void ddlDownPay_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlDataSource5.DataBind();
            if (!this.txtPrice.Text.Equals(string.Empty))
            {
                downPay = Convert.ToDecimal(this.txtPrice.Text) * (Convert.ToDecimal(ddlDownPay.SelectedItem.Text) / 100);
                this.txtPay.Text = downPay.ToString();
            }
        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            InterrestItem = InterrestInfoDAL.LoadInterrest(Convert.ToInt32(this.ddlMonth.SelectedValue.ToString()));
            this.txtInterest.Text = InterrestItem.INTERREST_RATE.ToString();
            totalAmount = (((carItem.CAR_PRICE - downPay) * InterrestItem.INTERREST_RATE) / 100) + (carItem.CAR_PRICE - downPay);
            this.txtPriceInterest.Text = decimal.Round(totalAmount, 2, MidpointRounding.AwayFromZero).ToString();
            this.txtPayMonth.Text = decimal.Round((totalAmount / InterrestItem.DEBT_MONTH)).ToString();
        }
    }
}